package com.meixiang.dialog;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.tool.Tool;
import com.meixiang.util.DensityUtil;
import com.nineoldandroids.view.ViewPropertyAnimator;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;


/**
 * 多项选择dialog
 */
public class SelectDialog extends Dialog {
    private IResultListener listener;
    private TextView mCancelTextView;
    private Context context;
    private View view;
    private List<String> data = new ArrayList<>();
    private ListView listView;

    /**
     * @param context
     * @param data
     * @param listener
     */
    public SelectDialog(Context context, String[] data, IResultListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        for (int i = 0; i < data.length; i++) {
            this.data.add(data[i]);
        }
        show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_select, null);
        setContentView(view);
        super.onCreate(savedInstanceState);
        initDialog();
        initView();
        event();
    }

    private void initDialog() {
        setCancelable(false);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        int width = Tool.getWindowWith(context);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    private void initView() {
        listView = ButterKnife.findById(this, R.id.select_dialog_list);
        listView.setAdapter(new MyAdapter());
        mCancelTextView = (TextView) findViewById(R.id.cancel);
    }

    private void event() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.SelectResult(position);
                dismiss();
            }
        });

        mCancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public interface IResultListener {
        /**
         * 返回选中的选项下标
         *
         *
         * @param position
         */
        void SelectResult(int position);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onStart() {
        ViewPropertyAnimator.animate(view).translationY(DensityUtil.dip2px(context, 235)).setDuration(0).start();
        ViewPropertyAnimator.animate(view).translationY(0).setDuration(300).start();
        super.onStart();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder mHolder=null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.dialog_item, null);
                mHolder=new ViewHolder(convertView);
                convertView.setTag(mHolder);
            }else{
                mHolder= (ViewHolder) convertView.getTag();
            }
            mHolder.option_one.setText(data.get(position));
            return convertView;
        }

        public class ViewHolder {
            public TextView option_one;

            public ViewHolder(View rootView) {
                this.option_one = (TextView) rootView.findViewById(R.id.option_one);
            }

        }
    }
}
